﻿using ACC.Extensions.ClientBusiness;
using ACC.Extensions.Entities;
using ACC.Extensions.PCClient.Helper;
using MahApps.Metro.Controls;
using System;
using System.IO;
using System.Windows;
using System.Windows.Input;

namespace ACC.Extensions.PCClient.View
{
    /// <summary>
    /// Interaction logic for ConfigPartPicWnd.xaml
    /// </summary>
    public partial class ConfigPartPicWnd : MetroWindow
    {
        public ConfigPartPicWnd()
        {
            InitializeComponent();
            this.Owner = Application.Current.MainWindow;
        }
        private void BaseMetroWindow_Loaded(object sender, RoutedEventArgs e)
        {
            dbgPartTypeList.ItemsSource = null;
            dbgPartTypeList.ItemsSource = UISession.PartTypePicList;
        }
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            ConfigPicUploadWnd uploadPicWnd = new ConfigPicUploadWnd();
            var dialogResult = uploadPicWnd.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
            {
                Dispatcher.BeginInvoke(new Action(() =>
                    {
                        dbgPartTypeList.ItemsSource = null;
                        dbgPartTypeList.ItemsSource = UISession.PartTypePicList;
                    }));
            }
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            if (dbgPartTypeList.SelectedItem != null)
            {
                string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "_pic\\" + (dbgPartTypeList.SelectedItem as PartTypePic).GridPicName;
                if (!File.Exists(strFilePath))
                {
                    MessageBox.Show("图片文件不存在");
                    return;
                }
                ConfigPicUploadWnd uploadPicWnd = new ConfigPicUploadWnd();
                uploadPicWnd.DisplayPartTypePic(dbgPartTypeList.SelectedItem as PartTypePic);
                var dialogResult = uploadPicWnd.ShowDialog();
                if (dialogResult.HasValue && dialogResult.Value)
                {
                    dbgPartTypeList.ItemsSource = null;
                    dbgPartTypeList.ItemsSource = UISession.PartTypePicList;
                }
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (dbgPartTypeList.SelectedItem != null)
            {
                PartTypePic selPartTypePic = dbgPartTypeList.SelectedItem as PartTypePic;
                string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "_pic\\" + selPartTypePic.GridPicName;
                if (File.Exists(strFilePath))
                {
                    File.Delete(strFilePath);
                }
                UISession.PartTypePicList.RemoveAll(x => x.PartNo == selPartTypePic.PartNo && x.PicSubNo ==selPartTypePic.PicSubNo);
                dbgPartTypeList.ItemsSource = null;
                dbgPartTypeList.ItemsSource = UISession.PartTypePicList;

                //报存图片到配置信息
                bool result = ClientSettingHelper.WritePicList(UISession.PartTypePicList);

                if (result)
                {
                    MessageDialogWnd.ShowDialog(
                         LanguageHelper.LoadStringFromResource("TITLE_MESSAGEDIALOG"),
                         LanguageHelper.LoadStringFromResource("MSG_STORE_SUCCESS"));
                }
                else
                {
                    MessageDialogWnd.ShowDialog(
                        LanguageHelper.LoadStringFromResource("TITLE_ERROR"),
                        LanguageHelper.LoadStringFromResource("MSG_ERR_SAVESETTING"));
                }
            }
        }

        private void dbgPartTypeList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (dbgPartTypeList.SelectedItem != null)
            {
                string strFilePath = AppDomain.CurrentDomain.BaseDirectory + "_pic\\" + (dbgPartTypeList.SelectedItem as PartTypePic).GridPicName;
                if (!File.Exists(strFilePath))
                {
                    MessageBox.Show("图片文件不存在");
                    return;
                }
                ConfigPicUploadWnd uploadPicWnd = new ConfigPicUploadWnd();
                uploadPicWnd.DisplayPartTypePic(dbgPartTypeList.SelectedItem as PartTypePic);
                uploadPicWnd.UploadPicDisable();
                uploadPicWnd.Show();
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void MetroWindow_Activated(object sender, EventArgs e)
        {

            UISession.IsWndActivated = this.IsActive;
        }

        private void MetroWindow_Deactivated(object sender, EventArgs e)
        {

            UISession.IsWndActivated = this.IsActive;
        }

        
    }
}
